<template>
  <div class="my-sz">
    <el-form
      :model="ruleForm"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
      :rules="rules"
    >
      <el-form-item label="用户名" prop="userName">
        <el-input v-model="ruleForm.userName" style="width:300px" :disabled="true"></el-input>
        <div class="name-tip">
          <span>剩余修改昵称次数：{{ruleForm.changeNameNum}}</span>
          <span class="btn" @click="centerDialogVisible=true" v-if="ruleForm.changeNameNum>0">修改</span>
        </div>
      </el-form-item>
      <el-form-item label="签名" prop="signature">
        <ElInput v-model="ruleForm.signature" placeholder="说点什么吧" type="textarea"></ElInput>
      </el-form-item>
      <el-form-item label="头像" prop="photo">
        <!-- <div class="photo">
            <img src="../../img/gan.png" alt>
        </div>-->
        <el-upload
          class="avatar-uploader"
          :action="baseUrl+'uploadImg'"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
        >
          <img v-if="imageUrl" :src="baseFilePath+ruleForm.photo" class="photo">
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
      <el-form-item label="生日" prop="birthday">
        <el-date-picker
          v-model="ruleForm.birthday"
          align="right"
          type="date"
          placeholder="选择日期"
          :picker-options="pickerOptions"
          value-format="yyyy-MM-dd"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="性别" prop="sex">
        <el-radio-group v-model="ruleForm.sex">
          <el-radio :label="1">男</el-radio>
          <el-radio :label="2">女</el-radio>
          <el-radio :label="0">保密</el-radio>
        </el-radio-group>
      </el-form-item>

      <el-form-item label="密码" prop="password">
        <span class="pwd">********</span>
        <ElButton type size="mini" @click="pwdDialogVisible=true">修改密码</ElButton>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">修改</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 修改昵称 -->
    <el-dialog title="请输入新的用户名" :visible.sync="centerDialogVisible" width="30%" center>
      <div class="new-name">
        <el-input v-model="newName"></el-input>
      </div>
      <div class="new-name-btn">
        <ElButton type="primary" @click="changeName">确认修改</ElButton>
      </div>
    </el-dialog>
    <!-- 修改密码 -->
    <el-dialog title="修改密码" :visible.sync="pwdDialogVisible">
      <el-form
        :model="pwdForm"
        status-icon
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="原始密码" prop="pwd">
          <el-input type="password" v-model="pwdForm.pwd" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="pwd1">
          <el-input type="password" v-model="pwdForm.pwd1" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="pwd2">
          <el-input type="password" v-model="pwdForm.pwd2" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="changePwd('ruleForm')">提交</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "my-sz",
  data: function() {
    return {
      baseUrl: this.GLOBAL.baseUploadPath,
      baseFilePath: this.GLOBAL.baseFilePath,
      centerDialogVisible: false,
      pwdDialogVisible: false,
      newName: "",
      ruleForm: {
        userName: "",
        birthday: "1990-01-01"
      },
      pwdForm: {
        pwd: "",
        pwd1: "",
        pwd2: "",
        account: this.$store.state.user.account
      },
      pickerOptions: {
        disabledDate(time) {
          return time.getTime() > +new Date();
        }
      },
      rules: {
        /* photo: [{ required: true, message: "图片不能为空!", trigger: "change" }] */
      },
      imageUrl: ""
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.axios.post("user/update", this.ruleForm).then(res => {
            if (res.data.success) {
              this.$message.success("修改成功！");
              this.$store.commit("setUser", res.data.user);
              localStorage.setItem("userData", JSON.stringify(res.data.user));
              this.ruleForm = res.data.user;
            }
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw);
      this.ruleForm.photo = res.src;
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isPng = file.type === "image/png";
      const isLt2M = file.size / 1024 / 1024 < 2;
      const isJPGPNG = isJPG || isPng;

      if (!isJPGPNG) {
        this.$message.error("上传封面图片只能是 JPG/PNG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传封面图片大小不能超过 2MB!");
      }

      return isJPGPNG && isLt2M;
    },
    //修改用户名称
    changeName() {
      this.axios
        .post("user/changeName", {
          id: this.$store.state.user.id,
          userName: this.newName
        })
        .then(res => {
          if (res.data.success) {
            this.$message.success("修改成功！");
            this.$store.commit("setUser", res.data.user);
            localStorage.setItem("userData", JSON.stringify(res.data.user));
            this.ruleForm = res.data.user;
            this.centerDialogVisible = false;
          }
        });
    },
    //修改密码
    changePwd() {
      this.axios
        .post("user/changePwd",this.pwdForm)
        .then(res => {
          if (res.data.success) {
            this.$message.success("修改成功！");
            this.$store.commit("removeUser");
            localStorage.removeItem("userData");
            this.$router.push({name:'login'})
          }
        });
    }
  },
  mounted: function() {
    // this.ruleForm.birthday='1996-09-06  '
  },
  created: function() {
    this.ruleForm = this.$store.state.user;
  }
};
</script>

<style lang="scss" scoped>
img {
  height: 100%;
  width: 100%;
}
.photo {
  width: 178px;
  height: 178px;
  display: block;
}
.pwd {
  margin: 20px;
}
.name-tip {
  color: #c0c4cc;
  font-size: 12px;
  .btn {
    text-decoration: underline;
    margin-left: 10px;
    cursor: pointer;
    &:hover {
      color: rgb(121, 121, 252);
    }
  }
}
.new-name {
  margin: 0 0 20px 0;
}
.new-name-btn {
  text-align: right;
}
</style>